//
//  Meeting_DetailViewController2.m
//  HuaRun2
//
//  Created by admin on 17/9/6.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "Meeting_DetailViewController2.h"
#import "Meeting_DetailModel.h"
#import "ImageLabView.h"
#import "LineView.h"
#import "KeyValueLabel.h"
#import "ImageMutLineLabelView.h"
#import "Meeting_DetailModel.h"
#import "Company_DetailPersonListViewController.h"
#import "Meeting_Detail_OutPerson.h"
#import "Meeting_Detail_In_PersonListViewController.h"
#import "Meeting_Detail_InPerson.h"
#import "NoteTableViewController.h"
#import "CuLabel.h"

@interface Meeting_DetailViewController2 ()

@property(nonatomic, strong) Meeting_DetailModel * detailModel;
//背景
@property (nonatomic, strong) UIView * backView1;
//背景
@property (nonatomic, strong) UIView * backView2;

//名字
@property (nonatomic, strong) UILabel * nameLab;
//查看备注
//note
@property (nonatomic, strong) UIView * noteView;
//外部与会人员
@property (nonatomic, strong) ImageLabView * contactView;
//内部与会人员
@property (nonatomic, strong) ImageLabView * meetingView;
//线1
@property (nonatomic, strong) LineView * line1;
//基本信息
@property (nonatomic, strong) UILabel * baseInfoLab;

//地区
@property (nonatomic, strong) ImageLabView * locationLabel;
//会议类型
@property (nonatomic, strong) KeyValueLabel * meetingTypeValueLabel;
//所属大会
@property (nonatomic, strong) KeyValueLabel * ownValueLabel;
//note
@property (nonatomic, strong) KeyValueLabel * noteView2;

//时区图片
@property (nonatomic, strong) UIImageView * timeImageView;
//时区、时间
@property (nonatomic, strong) CuLabel * timeLocCuLabel;

@end

@implementation Meeting_DetailViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewInit {
    [super viewInit];
    //    "meetings_detail"= "会议详情";//
    self.navigationItem.title = NSLocalizedString(@"meetings_detail", nil);
//    @"会议详情";
    self.view.backgroundColor = RGBA(249, 249, 249, 1);
    
    [self.view addSubview:self.backView1];
    [self.view addSubview:self.nameLab];
    [self.view addSubview:self.noteView];
    
    [self.view addSubview:self.contactView];
    [self.view addSubview:self.meetingView];
    [self.view addSubview:self.line1];
    self.contactView.hidden =YES;
    self.line1.hidden = YES;

    [self.view addSubview:self.baseInfoLab];
        
    [self.view addSubview:self.backView2];
//    _backView2.backgroundColor = kRandomColor;

    [self.view addSubview:self.timeImageView];
    [self.view addSubview:self.timeLocCuLabel];

    [self.view addSubview:self.locationLabel];
    
    [self.view addSubview:self.meetingTypeValueLabel];
    [self.view addSubview:self.ownValueLabel];
    
    [self.view addSubview:self.noteView2];

}

- (void)layoutInit {
    [super layoutInit];
    
    weakself(ws);
    [self.backView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.view).offset(kMargin10);
        make.right.mas_equalTo(ws.view).offset(-kMargin10);
        make.top.mas_equalTo(ws.view);
        make.height.mas_equalTo(145);
    }];
    
    [self.backView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(ws.backView1);
        make.top.mas_equalTo(ws.baseInfoLab.mas_bottom).offset(kMargin10);
        make.bottom.mas_equalTo(ws.noteView2.mas_bottom).offset(kMargin10);
    }];
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.backView1).offset(kMargin10);
        make.top.mas_equalTo(ws.backView1).offset(kMargin15);
        make.right.mas_equalTo(ws.noteView.mas_left).offset(-kMargin10);
    }];
    
    [self.noteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.nameLab);
        make.right.mas_equalTo(ws.backView1).offset(-kMargin10);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    
    [self.contactView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.backView1.mas_bottom).offset(-kMargin15);
        make.left.mas_equalTo(ws.nameLab).offset(0);
        make.right.mas_equalTo(ws.backView1.mas_centerX).offset(0);
//        make.width.mas_equalTo(ws.backView1).multipliedBy(0.5);
    }];
    
    [self.meetingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.contactView);
        make.left.mas_equalTo(ws.nameLab);
        make.width.mas_equalTo(ws.contactView);
    }];

    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(ws.contactView);
        make.width.mas_equalTo(1);
        make.centerX.mas_equalTo(ws.backView1);
    }];
    
    [self.baseInfoLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.backView1);
        make.top.mas_equalTo(ws.backView1.mas_bottom).offset(kMargin10);
        make.height.mas_equalTo(30);
    }];
    
    [self.timeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(ws.backView2).offset(kMargin10);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [self.timeLocCuLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.timeImageView.mas_right).offset(kMargin10);
        make.right.mas_equalTo(ws.backView2).offset(-kMargin10);
        make.top.mas_equalTo(ws.timeImageView);
    }];
    
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(ws.timeLable);
        make.left.mas_equalTo(ws.backView1).offset(kMargin10);
        make.right.mas_equalTo(ws.backView1).offset(-kMargin10);
        make.top.mas_equalTo(ws.timeLocCuLabel.mas_bottom).offset(kMargin10);
    }];
    
    [self.meetingTypeValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(ws.locationLabel);
        make.left.mas_equalTo(ws.backView1).offset(kMargin10 );
        make.top.mas_equalTo(ws.locationLabel.mas_bottom).offset(kMargin10);
    }];
    
    [self.ownValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(ws.meetingTypeValueLabel);
        make.top.mas_equalTo(ws.meetingTypeValueLabel.mas_bottom).offset(kMargin10);
    }];
    
    [self.noteView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(ws.meetingTypeValueLabel);
        make.top.mas_equalTo(ws.ownValueLabel.mas_bottom).offset(kMargin10);
    }];
}
- (void)methodBind {
    [self fetchMeetingDetail];
    [self fetchInContactPersonNum];
    [self fetchOutContactPersonNum];
    
    weakself(ws);
    [_noteView clickViewBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [ws seeNote];
    }];
    
    [_contactView clickViewBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [ws seeInnerPerson];
    }];
    
    [_meetingView clickViewBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [ws seeOutPerson];
    }];
    
}

- (void)seeNote {
    NoteTableViewController * customer = [NoteTableViewController new];
    customer.parentId = _detailModel.id ? _detailModel.id : @"";
    customer.noteType = NoteTypeMeeting;
    customer.name = [NSString showSpaceStringWithStr:self.detailModel.name];
    [self.navigationController pushViewController:customer animated:YES];

}

//查看内部与会人员
- (void)seeInnerPerson {
    if (_innerArray.count ==0) {
        [self showAlert:NSLocalizedString(@"暂无数据", nil)];
        return;
    }

    Meeting_Detail_In_PersonListViewController * customer = [Meeting_Detail_In_PersonListViewController new];
    customer.dataArray = _innerArray;
    customer.name = _detailModel.name;
    customer.meetingId = _meetingId;
//    customer.companyName = _detailModel.conferenceName;
    [self.navigationController pushViewController:customer animated:YES];
}

//查看外部与会人员
- (void)seeOutPerson {
    if (_outArray.count ==0) {
        [self showAlert:NSLocalizedString(@"暂无数据", nil)];
        return;
    }
    
    Company_DetailPersonListViewController * customer = [Company_DetailPersonListViewController new];
    customer.personListType = PersonListTypeYuHui;
    customer.dataArray = _outArray;
    customer.name = _detailModel.name;
    customer.meetingId = _meetingId ? _meetingId : @"";
//    customer.companyName = _detailModel.conferenceName;
    [self.navigationController pushViewController:customer animated:YES];
    
}



#pragma mark 网络请求
//获取会议详情
- (void)fetchMeetingDetail {
    
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl,
                      kMeeting_DetailUrl];
    NSString * accountId = _meetingId ? _meetingId : @"";
    NSDictionary * dic = @{@"meetingId": accountId};
    
    weakself(ws);
    [NetWorkHelper getWithURLString:url parameters:dic success:^(id object) {
        if (![object[@"code"] isEqual: kSuccessCode]) {
            NSString * info = [NSString isemptyStr:object[@"msg"]] ? kUnknownError : object[@"msg"];
            [ws showWithText:info];
            return;
        }
        Meeting_DetailModel * detailModel = [Meeting_DetailModel yy_modelWithJSON:object[@"obj"]];
        ws.detailModel = detailModel;
        [ws reConfigureInfo];
        
    } failure:^(NSError *error) {
        [ws showWithText:kNetworkError];
    }];
}

- (void)reConfigureInfo {
    
    _nameLab.text = [NSString showSpaceStringWithStr:self.detailModel.name];
 
    _locationLabel.nameLab.text = [NSString showSpaceStringWithStr:_detailModel.location];
    
    _meetingTypeValueLabel.valueLab.text = [NSString showSpaceStringWithStr:_detailModel.categories];
    _ownValueLabel.valueLab.text = [NSString showSpaceStringWithStr:_detailModel.conferenceName];
    _noteView2.valueLab.text = [NSString showSpaceStringWithStr:_detailModel.descriptionStr];
    
    NSString * text = [NSString showSpaceStringWithStr:[NSString
                                                       stringWithFormat:@"%@\n%@-%@",
                     _detailModel.timezone, _detailModel.startTime, _detailModel.endTime]];
    [_timeLocCuLabel resetLineSpaceHeight5WithStr:text];
}

//获取外部与会人员个数
- (void)fetchOutContactPersonNum {
    
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl, kMeeting_Detail_Out_PersonListUrl];
    NSString * accountId = _meetingId ? _meetingId : @"";
    
    NSDictionary * dic = @{@"meetingId": accountId};
    
    weakself(ws);
    [NetWorkHelper getWithURLString:url parameters:dic success:^(id object) {
        if (![object[@"code"] isEqual: kSuccessCode]) {
            NSString * info = [NSString isemptyStr:object[@"msg"]] ? kUnknownError : object[@"msg"];
            [ws showWithText:info];
            return;
        }
        
        NSArray * addressArray = [NSArray yy_modelArrayWithClass:[Meeting_Detail_OutPerson class]
                                                            json:object[@"list"]];
        ws.outArray = addressArray;

//        if (addressArray.count > 0) {        
            //        NSLocalizedString(@"meetings_member_out", nil);
            ws.meetingView.infoStr = [NSString stringWithFormat:@"%@(%ld)",NSLocalizedString(@"meetings_member_out", nil),
                                      [object[@"list"] count]];
//        }
        
    } failure:^(NSError *error) {
        [ws showWithText:kNetworkError];
    }];
    
}

//获取内部与会人员个数
- (void)fetchInContactPersonNum {
    
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl,
                      kMeeting_Detail_In_PersonListUrl];
    
    NSString * accountId = _meetingId ? _meetingId: @"";
    NSDictionary * dic = @{@"meetingId": accountId};
    
    weakself(ws);
    [NetWorkHelper getWithURLString:url parameters:dic success:^(id object) {
        if (![object[@"code"] isEqual: kSuccessCode]) {
            NSString * info = [NSString isemptyStr:object[@"msg"]] ? kUnknownError : object[@"msg"];
            [ws showWithText:info];
            return;
        }
        
        NSArray * addressArray = [NSArray yy_modelArrayWithClass:[Meeting_Detail_InPerson class]
                                                            json:object[@"list"]];
        ws.innerArray = addressArray;
        
//        if (addressArray.count > 0) {
            //        NSLocalizedString(@"meetings_member_in", nil); 内部与会人员
            ws.contactView.infoStr = [NSString stringWithFormat:@"%@(%ld)",
                                      NSLocalizedString(@"meetings_member_in", nil),
                                      [object[@"list"] count]];
//        }
    } failure:^(NSError *error) {
        [ws showWithText:kNetworkError];
    }];
    
}
#pragma mark - getter setter
- (UILabel *)baseInfoLab {
    if (!_baseInfoLab) {
        self.baseInfoLab = [UILabel new];
        _baseInfoLab.textAlignment = NSTextAlignmentLeft;
        _baseInfoLab.font = kFontSize15;
        _baseInfoLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
        _baseInfoLab.text = NSLocalizedString(@"基本信息", nil);
    }
    return _baseInfoLab;
}

- (UILabel *)nameLab {
    if (!_nameLab) {
        self.nameLab = [UILabel new];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        _nameLab.font = kFontSize15;
        _nameLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
        _nameLab.backgroundColor = kRandomColor;
    }
    return _nameLab;
}

//"meetings_member_in"= "内部与会人员";
- (ImageLabView *)contactView {
    if (!_contactView) {
        self.contactView = [ImageLabView new];
        _contactView.iconView.image = [UIImage imageNamed:@"内部人员"];
        _contactView.infoStr = NSLocalizedString(@"meetings_member_in", nil);
//        @"内部与会人员";
        _contactView.backgroundColor = kRandomColor;
    }
    return _contactView;
}

//"meetings_member_out"= "外部与会人员";
- (ImageLabView *)meetingView {
    if (!_meetingView) {
 
        self.meetingView = [ImageLabView new];
        //        _meetingView.imageSize = CGSizeMake(25, 25);
        _meetingView.iconView.image = [UIImage imageNamed:@"联系人001"];
        _meetingView.infoStr = NSLocalizedString(@"meetings_member_out", nil);

//        @"外部与会人员";
        _meetingView.backgroundColor = kRandomColor;
    }
    return _meetingView;
}

- (UIView *)backView1 {
    if (!_backView1) {
        self.backView1 = [UIView new];
        _backView1.backgroundColor = [UIColor whiteColor];
    }
    return _backView1;
}

- (UIView *)backView2 {
    if (!_backView2) {
        self.backView2 = [UIView new];
        _backView2.backgroundColor = [UIColor whiteColor];
    }
    return _backView2;
}
- (LineView *)line1 {
    if (!_line1) {
        self.line1 = [LineView new];
    }
    return _line1;
}
- (ImageLabView *)locationLabel {
    if (!_locationLabel) {
        self.locationLabel = [ImageLabView new];
        //        _locationLabel.backgroundColor = kRandomColor;
//        _locationLabel.nameLab.text = kTestStr;
        _locationLabel.iconView.image = [UIImage imageNamed:@"地址_1"];
    }
    return _locationLabel;
}

//"meeting_type"= "会议类型";//
- (KeyValueLabel *)meetingTypeValueLabel {
    if (!_meetingTypeValueLabel) {
        self.meetingTypeValueLabel = [[KeyValueLabel alloc] init];
        _meetingTypeValueLabel.valueLab.text = @" ";
        _meetingTypeValueLabel.keyLab.text = NSLocalizedString(@"meeting_type", nil);
//        @"会议类型";
        _meetingTypeValueLabel.keyLab.textAlignment = NSTextAlignmentLeft;
    }
    return _meetingTypeValueLabel;
}
//"parent_meeting"= "所属大会";//
- (KeyValueLabel *)ownValueLabel {
    if (!_ownValueLabel) {
        self.ownValueLabel = [[KeyValueLabel alloc] init];
        _ownValueLabel.valueLab.text = @" ";
        _ownValueLabel.keyLab.text = NSLocalizedString(@"parent_meeting", nil);
        _ownValueLabel.keyLab.textAlignment = NSTextAlignmentLeft;
//        @"所属大会";
    }
    return _ownValueLabel;
}

//meetings_remarks"= "会议备注";//
- (KeyValueLabel *)noteView2 {
    if (!_noteView2) {
        self.noteView2 = [KeyValueLabel new];
        _noteView2.valueLab.text = @" ";
        _noteView2.keyLab.text = NSLocalizedString(@"meetings_remarks", nil);
//        @"会议备注";
        _noteView2.keyLab.textAlignment = NSTextAlignmentLeft;
    }
    return _noteView2;
}
- (UIView *)noteView {
    if (!_noteView) {
        self.noteView = [self makeCommonLabelWithText:NSLocalizedString(@"查看备注", nil)
                                            imageName:@"最近查看"
                                            imageSize:(CGSizeMake(15, 10))];
    }
    return _noteView;
}

- (UIView *)makeCommonLabelWithText:(NSString *)text
                          imageName:(NSString *)imageName
                          imageSize:(CGSize )imageSize{
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = kRandomColor;
    view.frame = CGRectMake(0, 0, 100, 20);
    
    UIImageView * iconView = [UIImageView new];
    iconView.image = [UIImage imageNamed:imageName];
    //    iconView.frame = CGRectMake(0, 2.5, 15, 15);
    iconView.frame = CGRectMake(0, (20 - imageSize.height ) / 2, imageSize.width, imageSize.height);
    [view addSubview:iconView];
    
    UILabel * companyLab = [UILabel new];
    companyLab.textAlignment = NSTextAlignmentLeft;
    companyLab.frame = CGRectMake(20, 0, 100- imageSize.width, 20);
    companyLab.font = kGeneralFontSize;
    companyLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
    companyLab.text = text;
    [view addSubview:companyLab];
    
    return view;
}

- (CuLabel *)timeLocCuLabel {
    if (!_timeLocCuLabel) {
        self.timeLocCuLabel = [CuLabel new];
        _timeLocCuLabel.numberOfLines = 0;
    }
    return _timeLocCuLabel;
}

- (UIImageView *)timeImageView {
    if (!_timeImageView) {
        self.timeImageView = [UIImageView new];
        _timeImageView.image = [UIImage imageNamed:@"时间_1"];
    }
    return _timeImageView;
}

@end
